package animation4.view.zhaolixiang.com.viewanimation4

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.ValueAnimator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_view_animation.setOnClickListener{
            val translateAnimation=TranslateAnimation(Animation.ABSOLUTE,0f,
                    Animation.ABSOLUTE,200f,
                    Animation.ABSOLUTE,0f,
                    Animation.ABSOLUTE,200f)
            translateAnimation.duration=3000
            translateAnimation.fillAfter=true
            view.startAnimation(translateAnimation)
        }
        view.setOnClickListener{
            Log.e("ViewAnimation","点击了")
        }
        bt_vialue_animator.setOnClickListener{
            val valueAnimator=ValueAnimator.ofInt(200,500)
            valueAnimator.duration=5000
            valueAnimator.addUpdateListener { animator ->
                view.layout(animator.animatedValue as Int,
                        animator.animatedValue as Int,
                        animator.animatedValue as Int+view.width,
                        animator.animatedValue as Int+view.height)
            }
            valueAnimator.start()
        }

        bt_vialue_animator2.setOnClickListener{
            //创建多个动画区间
            val valueAnimator=ValueAnimator.ofInt(100,400,200,500)
            valueAnimator.duration=5000
            valueAnimator.addUpdateListener {
                animator->
                view.layout(animator.animatedValue as Int,
                        animator.animatedValue as Int ,
                        animator.animatedValue as Int+ view.width,
                        animator.animatedValue as Int +view.height)
            }
            valueAnimator.start()
        }

        bt_vialue_animator_repeat_restart.setOnClickListener{
            val valueAnimator=ValueAnimator.ofFloat(100f,500f,200f,600f)
            valueAnimator.duration=5000
            //循环三次
            valueAnimator.repeatCount=3
            //设置循环模式:restart,reverse
            valueAnimator.repeatMode=ValueAnimator.RESTART
            valueAnimator.addUpdateListener {
                animator->
                view.layout(animator.animatedValue.toString().toFloat().toInt(),
                        animator.animatedValue.toString().toFloat().toInt(),
                        animator.animatedValue.toString().toFloat().toInt()+view.width,
                        animator.animatedValue.toString().toFloat().toInt() + view.height)
            }
            valueAnimator.start()
        }
        val valueAnimator=ValueAnimator.ofFloat(100f,500f,200f,600f)
        valueAnimator.duration=5000
        valueAnimator.repeatCount=3
        valueAnimator.repeatMode=ValueAnimator.REVERSE
        val updateListener=ValueAnimator.AnimatorUpdateListener {
            animator->
            view.layout(animator.animatedValue.toString().toFloat().toInt(),
                    animator.animatedValue.toString().toFloat().toInt(),
                    animator.animatedValue.toString().toFloat().toInt() +view.width,
                    animator.animatedValue.toString().toFloat().toInt() +view.height)
        }
        valueAnimator.addUpdateListener(updateListener)
        val listener= object:AnimatorListener{
            override fun onAnimationRepeat(p0: Animator?) {
                Log.e("valueAnimation状态改变","onAnimationRepeat")
            }

            override fun onAnimationEnd(p0: Animator?) {
                Log.e("valueAnimation状态改变","onAnimationEnd")
            }

            override fun onAnimationCancel(p0: Animator?) {
                Log.e("valueAnimation状态改变","onAnimationCancel")
            }

            override fun onAnimationStart(p0: Animator?) {
                Log.e("valueAnimation状态改变","onAnimationStart")
            }

        }
        valueAnimator.addListener(listener)
        bt_vialue_animator_cance.setOnClickListener{
            //取消动画
            valueAnimator.cancel()
        }
        bt_vialue_animator_repeat_revert.setOnClickListener{
            //开始动画
            valueAnimator.start()
        }
        bt_vialue_animator_remove_update_listener.setOnClickListener{
            //移除单个监听
            valueAnimator.removeUpdateListener(updateListener)
        }
        bt_vialue_animator_remove_all_update_listener.setOnClickListener{
            //移除所有
            valueAnimator.removeAllUpdateListeners()
        }
        bt_vialue_animator_remove_listener.setOnClickListener{
            valueAnimator.removeListener(listener)
        }
        bt_vialue_animator_remove_all_listener.setOnClickListener{
            valueAnimator.removeAllListeners()
        }
        bt_vialue_animator_delay_clone.setOnClickListener{
            //clone一个新对象
            val valueAnimation2=valueAnimator.clone()
            //设置三秒后再执行
            valueAnimation2.startDelay=3000
            valueAnimation2.start()
        }
    }
}
